package com.yjps.house.service;

import com.yjps.common.dao.AddressDao;
import com.yjps.common.model.Address;
import com.yjps.common.page.Pager;
import com.yjps.house.dao.BuildingDao;
import com.yjps.house.model.Building;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service("buildingService")
public class BuildingService {

    @Autowired
    BuildingDao buildingDao;

    @Autowired
    AddressDao addressDao;

    @Transactional
    public List<Building> getList(int agentID) {
        String hql = "from Building b where b.isValid=1 and b.agentsId="+agentID;
        return buildingDao.find(hql);

    }

    @Transactional
    public List<Building> getListByAgent(int agentID) {
        String hql = "from Building b where b.isValid=1 and b.agentsId = ( select s.userId from Shop s where s.id = (select a.shopId from Agent a where a.id ="+agentID+"))";
        return buildingDao.find(hql);

    }

    @Transactional
    public void delete(int id) {
        Building building = (Building) buildingDao.get(id);
        buildingDao.invalid(building);

    }

    @Transactional
    public int add(Building building, int addressId) {

        building.setAddressId(addressId);
        building.setState(1);

        return buildingDao.save(building);

    }

    @Transactional
    public int addAddress(Address address) {

        return addressDao.save(address);
    }

    public Address findAddress(int id){
        return addressDao.load(id);
    }

    @Transactional
    public boolean update( int buildingId,String title,String description,String imageUrl,int averagePrice,String houseType) {
        Building localBuilding = buildingDao.get(buildingId);
        System.out.println(description);


        if (localBuilding==null ) {
            return false;
        }else if(localBuilding.getIsValid() == 0){
            return  false;
        }
        String hql = "update Building b set b.title = :title , b.description = :description , b.imageUrl = :imageUrl , b.averagePrice = :averagePrice , b.houseType = :houseType where b.id = :id";
        Map<String,Object> params = new HashMap<>();
        params.put("id",buildingId);
        if(title!=null)
            params.put("title", title);
        else
            params.put("title", localBuilding.getTitle());
        if(description!=null)
            params.put("description", description);
        else
            params.put("description", localBuilding.getDescription());
        if(imageUrl!=null)
            params.put("imageUrl", imageUrl);
        else
            params.put("imageUrl", localBuilding.getImageUrl());
        if(houseType!=null)
            params.put("houseType", houseType);
        else
            params.put("houseType", localBuilding.getHouseType());
        if(averagePrice!=0)
            params.put("averagePrice", averagePrice);
        else
            params.put("averagePrice", localBuilding.getAveragePrice());

        buildingDao.excute(hql,params);
        /*
        localBuilding.setTitle(title);
        localBuilding.setDescription(description);
        localBuilding.setImageUrl(imageUrl);
        localBuilding.setAveragePrice(averagePrice);
        localBuilding.setHouseType(houseType);*/
        //buildingDao.update(localBuilding);

        return true;

    }

    public Pager<Building> queryBuildingByPager(Pager<Building> pager,String city, String averagePrice, String houseType) {
        String hql = "from Building t where t.isValid=1 and t.state=1";
        Map<String,Object> params = new HashMap();

        if(averagePrice!=null&&!averagePrice.equals("")){
            String[] str = averagePrice.split("-");
            int start = Integer.parseInt(str[0]);
            int end = Integer.parseInt(str[1]);

            hql = hql + " and t.averagePrice between :start and :end ";

            params.put("start",start);
            params.put("end",end);
        }
        if(houseType!=null&&!houseType.equals("")){
            hql = hql + " and t.houseType like " +"'%"+ houseType  +"%' ";
//            params.put("houseType",houseType);
        }
        if(city!=null&&!city.equals("")){
            hql = hql + " and t.addressId in (select a.id from Address a where a.city =:city)";
            params.put("city",city);

        }
        List<Building> accounts = buildingDao.findWithSelect(hql, params, (pager.getPageNo()-1)*pager.getPageSize(), pager.getPageSize());
        System.out.println("pagrno >> "+pager.getPageNo());
        pager.setCount(buildingDao.getTotalCount(hql, params));
        pager.setList(accounts);
        return pager;

    }
}
